package com.elepant.tank;

import java.awt.*;

public class Bullet {

    public static int WIDTH = ResourceImage.bulletD.getWidth();
    public static int HEIGHT = ResourceImage.bulletD.getHeight();

    private int x = 0;//子弹出现的起始位置
    private int y = 0;//子弹出现的起始位置
    private Direction direction = Direction.Right;
    private static final int Speed = 15;

    private boolean living = true;

    private Group group;
    private Rectangle rectangle = new Rectangle();//一个子弹对应一个矩形,然后在子弹移动后，更新矩形的坐标即可
    private TankFrame tankFrame;


    public Bullet(int x, int y, Direction direction, Group group, TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.direction = direction;
        this.group = group;
        this.tankFrame = tankFrame;

        //初始化矩形
        rectangle.x = this.x;
        rectangle.y = this.y;
        rectangle.width = WIDTH;
        rectangle.height = HEIGHT;

    }

    public void paint(Graphics graphics) {

        System.out.println("bullet x:" + x + " y:" + y);
        /*Color c = graphics.getColor();
        graphics.setColor(Color.red);
        graphics.fillOval(x, y, 30, 30);
        graphics.setColor(c);*/
        /*x += 10;
        y += 10;*/

        if (!living) {
            this.tankFrame.bulletList.remove(this);
        }

        switch (direction) {
            case Up:
                graphics.drawImage(ResourceImage.bulletU, x, y, null);
                break;
            case Down:
                graphics.drawImage(ResourceImage.bulletD, x, y, null);
                break;
            case Left:
                graphics.drawImage(ResourceImage.bulletL, x, y, null);
                break;
            case Right:
                graphics.drawImage(ResourceImage.bulletR, x, y, null);
                break;
            default:
                break;

        }

        moving();

    }

    private void moving() {
        switch (direction) {
            case Up:
                y -= Speed;
                break;
            case Down:
                y += Speed;
                break;
            case Left:
                x -= Speed;
                break;
            case Right:
                x += Speed;
                break;
            default:
                break;

        }

        //更新矩形坐标
        rectangle.x = this.x;
        rectangle.y = this.y;

        //当移除边界的时候，则将 living = false
        if (x < 0 || y < 0 || x > TankFrame.ScreenWidth || y > TankFrame.ScreenHeight) {
            living = false;
        }
    }

    //子弹和坦克做碰撞检测
    public void colliedWith(Tank tank) {
        if (this.group == tank.getGroup()) return;
        //子弹矩形
        //Rectangle rectangleBullet = new Rectangle(this.x, this.y, WIDTH, HEIGHT);

        //Tank 矩形
        //Rectangle rectangleTank = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);

        if (rectangle.intersects(tank.rectangle)) {
            tank.die();
            this.die(); //子弹die

            //产生爆炸效果
            int eX = tank.getX() + Tank.WIDTH / 2 - Explode.WIDTH / 2;
            int eY = tank.getY() + Tank.HEIGHT / 2 - Explode.HEIGHT / 2;
            this.tankFrame.explodes.add(new Explode(eX, eY, this.tankFrame));
        }
    }

    private void die() {
        this.living = false;
    }
}
